package com.beg.cant.controller;

import com.beg.cant.comment.result.Result;
import com.beg.cant.dto.PageRequest;
import com.beg.cant.dto.PageResponse;
import com.beg.cant.entity.Post;
import com.beg.cant.service.PostService;

import java.io.IOException;
import java.lang.Long;

import com.beg.cant.vo.PostVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/posts")
@Tag(name = "动态管理", description = "动态的增删改查接口")
public class PostController {
    @Autowired
    private PostService postService;
    @PostMapping
    @Operation(summary = "创建动态", description = "创建一个新的动态")
    public Result<String> createPost(@RequestBody Post post) throws IOException {
        postService.savePostsById(post);
        return Result.success("动态创建成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取动态", description = "根据ID获取动态信息")
    public Result<PostVO> getPost(@Parameter(description = "动态ID") @PathVariable Long id) {
        return Result.success(postService.getPostsById(id));
    }

    @GetMapping("/id/{id}")
    @Operation(summary = "获取动态", description = "根据ID获取动态信息")
    public Result<List<PostVO>> getUserPost(@Parameter(description = "用户ID") @PathVariable Long id) {
        return Result.success(postService.getUserPostsById(id));
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新动态", description = "根据ID更新动态信息")
    public Result<String> updatePost(@RequestBody Post post) {
        postService.updateById(post);
        return Result.success("动态更新成功");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除动态", description = "根据ID删除动态")
    public Result<String> deletePost(@Parameter(description = "动态ID") @PathVariable Long id) {
        postService.removeById(id);
        return Result.success("动态删除成功");
    }

    @GetMapping("/list")
    @Operation(summary = "获取所有动态", description = "获取所有动态的列表，支持分页查询")
    public Result<PageResponse<PostVO>> getAllPosts(PageRequest pageRequest) {
        return Result.success(postService.getPostslist(pageRequest));
    }
}
